package com.jse.util;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.catalina.User;

import com.jse.entity.Email;
import com.jse.lang.Mirror;
import com.jse.lang.inject.Injecting;

public class Objs {

	public static <T>T create(Class<T> clazz) {
			try {
				return (T) clazz.getConstructor().newInstance();
			} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException | SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
	}
	
	public static Object create(String clazzName) {
		try {
		return Class.forName(clazzName).getConstructor().newInstance();
		} catch (SecurityException | ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException e) {
			return null;
		}
	}
	
	public static <T>T create(Class<T> clazz,JseMap map){
		Mirror<T> mirror = Mirror.me(clazz);
		T t = mirror.born();
		for (Entry<String, Object> en : ((Map<String, Object>) map).entrySet()) {
			String name = en.getKey();
			Injecting setter = mirror.getInjecting(name);
			setter.inject(t, en.getValue());
		}
		return t;
	}
	public static Object create(String clazz,JseMap map){
		try {
			return create(Class.forName(clazz),map);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[] args) {
		Email em=create(Email.class,new JseMap("account","1").add("host", "都治好"));
		System.err.println(em);
	}
}
